Repository navigation
Create streamed download targets exclusively - #403
Closed
ryanduguid wants to merge 2 commits into
Closed
ryanduguid wants to merge 2 commits into
ryanduguid wants to merge 2 commits into
Conversation
Use os.open with O_CREAT | O_EXCL instead of the 'x' file mode, and map EEXIST from that call to the existing StreamingError, as Python 2.7 has neither the 'x' mode nor raise ... from. Wrap the descriptor with os.fdopen separately and close it if wrapping fails.
Author
|
Closing this as part of a tidy-up of my open PRs. Happy to reopen or rebase it if it's still useful. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Open a streamed download destination exclusively, preserving the existing refusal to overwrite when another writer creates the path after validation.
The file is created with
os.open(..., O_WRONLY | O_CREAT | O_EXCL)and then wrapped withos.fdopen, so the check and the creation are one step on every Python version the package declares, including 2.7. An existing file still raisesStreamingError("File already exists: ..."); otherOSErrors propagate unchanged. If wrapping the descriptor fails, it is closed and the original error is re-raised.Evidence
After: The download-creation tests pass, covering creation of a new file, a caller-owned stream, preservation of the winning writer's bytes and closing the descriptor when wrapping it fails. The race test fails on
main.flake8 requests_toolbelt(thepy27-flake8environment) is also clean. On 2.7,tests/test_x509_adapter.pyfails to collect because it importsdatetime.timezone; that is unchanged by this PR and happens onmaintoo.Risk
A concurrent creator now causes the download to fail instead of replacing its file.
Unverified
PyPy, macOS and the hosted CI matrix were not run.